﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DALFactory;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class UserMstBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IUserCustomerDA UsercustomerDA = DataAccess.CreateUserCustomerDA();
        private static readonly IUserMstDA userDA = DataAccess.CreateUserMstDA();
        private static readonly IUserOwnerDA userOwnerDA = DataAccess.CreateUserOwnerDA();
        private static readonly IUserRoleDA UserRoleDA = DataAccess.CreateUserRoleDA();

        public UserMstBLL()
        {
            this.pubParasInfo = null;
            this.dataBaseID = new List<string>();
            this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
        }

        public UserMstBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public UserMstBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = new List<string>();
                this.dataBaseID.Add(ConstUtils.CONST_GLOBAL_TABLE);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> UserIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<UserMstInfo> userMstInfos = new List<UserMstInfo>();
                UserMstInfo userMstInfo = null;
                for (i = 0; i < UserIDS.Count; i++)
                {
                    userMstInfo = userDA.GetUserMstByID(dataBase, tran, UserIDS[i], this.dataBaseID[0]);
                    userMstInfos.Add(userMstInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (userDA.DeleteUserMst(dataBase, tran, UserIDS, this.dataBaseID) > 0)
                {
                    for (i = 0; i < UserIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<UserMstInfo>(dataBase, tran, userMstInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "USER_MST", "wh1.");
                        temp = temp + UserIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return userDA.GetAllUserMst(this.dataBaseID[0]);
        }

        public object GetInfoByID(string userID)
        {
            UserMstInfo userInfo = userDA.GetUserMstByID(userID, this.dataBaseID[0]);
            if (userInfo != null)
            {
                DbConfigInfo dbconfig = DataAccess.CreateUserWhDA().GetUserWhByUserId(userInfo.UserID);
                userInfo.Passwd = new EnDeCrypt("pwd").DeCrypt(userInfo.Passwd);
                userInfo.CurrentWH = dbconfig.WhDbName;
                userInfo.IsGlobal = dbconfig.IsGlobal;
                userInfo.WhDbDesc = dbconfig.WhDbDesc;
                OwnerInfo userOwnerInfo = userOwnerDA.GetOwnerByUserID(userInfo.UserID);
                userInfo.OwnerID = userOwnerInfo.OwnerID;
                UserCustomerInfo customerInfo = UsercustomerDA.GetCustomerByUserID(userInfo.UserID);
                userInfo.CustomerID = customerInfo.CustomerID;
                userInfo.UserRoleInfos = UserRoleDA.GetUserRoleByUserID(userInfo.UserID, this.dataBaseID[0]);
                if (userDA.CheckUserFunctionID(userID, "1031", "41", this.dataBaseID[0]))
                {
                    userInfo.IsPremissionsFunctionID = "Y";
                }
            }
            return userInfo;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            UserMstQueryEntity userQuery = obj as UserMstQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(userQuery.UserID))
            {
                sqlWhere.Add(" USER_ID " + userQuery.UserID);
            }
            if (!string.IsNullOrEmpty(userQuery.UserName))
            {
                sqlWhere.Add(" USER_Name " + userQuery.UserName);
            }
            if (!string.IsNullOrEmpty(userQuery.City))
            {
                sqlWhere.Add(" CITY " + userQuery.City);
            }
            if (!string.IsNullOrEmpty(userQuery.Country))
            {
                sqlWhere.Add(" COUNTRY " + userQuery.Country);
            }
            if (!string.IsNullOrEmpty(userQuery.Department))
            {
                sqlWhere.Add(" DEPARTMENT " + userQuery.Department);
            }
            if (!string.IsNullOrEmpty(userQuery.Email))
            {
                sqlWhere.Add(" EMAIL " + userQuery.Email);
            }
            if (!string.IsNullOrEmpty(userQuery.Fax))
            {
                sqlWhere.Add(" FAX " + userQuery.Fax);
            }
            if (!string.IsNullOrEmpty(userQuery.Mobile))
            {
                sqlWhere.Add(" MOBILE " + userQuery.Mobile);
            }
            if (!string.IsNullOrEmpty(userQuery.State))
            {
                sqlWhere.Add(" STATE " + userQuery.State);
            }
            if (!string.IsNullOrEmpty(userQuery.Zip))
            {
                sqlWhere.Add(" ZIP " + userQuery.Zip);
            }
            if (!string.IsNullOrEmpty(userQuery.Phone))
            {
                sqlWhere.Add(" PHONE " + userQuery.Phone);
            }
            if (!string.IsNullOrEmpty(userQuery.IsWhUser))
            {
                sqlWhere.Add(" IS_WH_USER " + userQuery.IsWhUser);
            }
            return userDA.GetUserMstByQueryList(sqlWhere, userQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string UserID = string.Empty;
            try
            {
                UserMstInfo UserMst = obj as UserMstInfo;
                if (userDA.CheckUserMstIDUnique(UserMst.UserID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F1200_001", this.pubParasInfo.UserLanguage, new List<string> { UserMst.UserID });
                }
                DataSet ds = commonMethod.GetCurrentUserAndWHCount();
                if (((ds != null) && (ds.Tables.Count > 0)) && (int.Parse(ds.Tables[0].Rows[0][0].ToString()) >= UserMst.MaxUsers))
                {
                    throw new Exception("用户数已达到系统注册用户数上限,不可再新增!");
                }
                UserMst.Passwd = new EnDeCrypt("pwd").EnCrypt(UserMst.Passwd);
                UserMst.CreatedDate = DateTime.Now;
                UserMst.UpdatedDate = DateTime.Now;
                UserMst.UpdatedBy = UserMst.CreatedBy;
                if (userDA.InsertUserMst(UserMst, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UserMstInfo>(null, UserMst, AuditAction.Insert, this.pubParasInfo.UserID, "USER_MST", "wh1.");
                    UserID = UserMst.UserID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return UserID;
        }

        public string Update(object obj)
        {
            string UserID = string.Empty;
            try
            {
                EnDeCrypt endeCrypt = new EnDeCrypt("pwd");
                UserMstInfo UserMst = obj as UserMstInfo;
                UserMst.UpdatedDate = DateTime.Now;
                UserMst.Passwd = endeCrypt.EnCrypt(UserMst.Passwd);
                UserMstInfo oldUserMstInfo = userDA.GetUserMstByID(UserMst.UserID, this.dataBaseID[0]);
                oldUserMstInfo.Passwd = endeCrypt.EnCrypt(oldUserMstInfo.Passwd);
                if (userDA.UpdateUserMst(UserMst, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<UserMstInfo>(oldUserMstInfo, UserMst, AuditAction.Update, this.pubParasInfo.UserID, "USER_MST", "wh1.");
                    UserID = UserMst.UserID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return UserID;
        }
    }
}

